

package cn.shengchao.examstar.distribute.ohs.remote.rpc;

import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.distribute.acl.ports.repositories.IDistributeRepository;
import cn.shengchao.examstar.distribute.infrastrure.query.SaleAmountQuery;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeOrderTaskSaleService;
import cn.shengchao.examstar.distribute.infrastrure.vo.DistributeSale;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeTypeEnum;
import cn.shengchao.examstar.rpc.member.api.IMemberRpc;
import cn.shengchao.examstar.rpc.member.req.MemberQueryPage;
import cn.shengchao.examstar.rpc.member.res.MemberList;
import cn.shengchao.examstar.rpc.uddsys.bi.PlatformUserArchBI;
import cn.shengchao.examstar.rpc.uddsys.req.DistributeNodeInfoStateReq;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletableFuture;


@DubboService
public class MemberRpc implements IMemberRpc {

    @Autowired
    private IDistributeRepository distributeRepository;

    @Autowired
    private DistributeNodeInfoService distributeNodeInfoService;

    @Autowired
    private DistributeOrderTaskSaleService distributeOrderTaskSaleService;

    @Autowired
    private PlatformUserArchBI platformUserArchBI;

    @Override
    public PageResult<MemberList> getMemberList(MemberQueryPage memberQueryPage) {
        Page<Object> page = PageHelper.startPage(memberQueryPage.getPage(), memberQueryPage.getSize());
        List<MemberList> memberList = distributeRepository.getMemberList(memberQueryPage);
        List<CompletableFuture<MemberList>> completableFutures = memberList.stream().map(item -> CompletableFuture
                .supplyAsync(() ->
                        {
                            SaleAmountQuery saleAmountQuery = new SaleAmountQuery();
                            saleAmountQuery.setDistributeUserId(item.getUserId());
                            DistributeSale distributeSale = distributeOrderTaskSaleService.getSaleAmount(saleAmountQuery);
                            if (distributeSale != null) {
                                item.setEarnings(distributeSale.getAmount());
                            } else {
                                item.setEarnings(BigDecimal.ZERO);
                            }
                            return item;
                        }
                )).toList();
        CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).join();
        return PageResult.of(page.getTotal(), memberList);
    }

    @Override
    public void changeStatus(Long memberId, Integer status) {
        DistributeNodeInfoStateReq distributeNodeInfoStateReq = new DistributeNodeInfoStateReq();
        distributeNodeInfoStateReq.setUserId(memberId);
        distributeNodeInfoStateReq.setEnableStatus(status);
        distributeNodeInfoStateReq.setDistributeType(DistributeTypeEnum.TYPE_SALE.getType());
        platformUserArchBI.updateNodeState(distributeNodeInfoStateReq);
    }
}
